package com.cs.base.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件名:    BaseRVAdapter
 * 描述:     TODO 结合dataBinding的RecyclerView Adapter
 */

public abstract class BaseRVAdapter<Data, VB extends ViewBinding> extends RecyclerView.Adapter<BaseViewHolder<VB>> {

    private List<Data> data;
    protected Context context;
    protected OnItemClickListener<Data> listener;


    public BaseRVAdapter() {
        data = new ArrayList<>();
    }

    public BaseRVAdapter(List<Data> data) {
        this.data = data == null ? new ArrayList<Data>() : data;
    }


    @NonNull
    @Override
    public BaseViewHolder<VB> onCreateViewHolder(@NonNull ViewGroup parent, int position) {
        this.context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        VB binding = getViewBinding(inflater, parent);
        BaseViewHolder<VB> baseViewHolder = new BaseViewHolder<>(binding.getRoot());
        baseViewHolder.setBinding(binding);
        return baseViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder<VB> holder, final int position) {
        VB binding = holder.getBinding();
        final Data itemData = data.get(position);
        onBindViewHolder(itemData, binding, position);
        //设置点击事件
        if (listener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemClick(itemData, position);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return listener.onItemLongClick(itemData, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    /**
     * 获取子类布局ViewBinding
     */
    protected abstract VB getViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container);

    /**
     * 绑定数据
     */
    protected abstract void onBindViewHolder(Data data, VB binding, int position);

    /**
     * 设置新数据
     *
     * @param data
     */
    public void setNewData(List<Data> data) {
        this.data.clear();
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    /**
     * 添加数据
     *
     * @param data
     */
    public void addData(Data data) {
        this.data.add(data);
        notifyDataSetChanged();
    }

    /**
     * 添加数据
     *
     * @param data
     */
    public void addData(List<Data> data) {
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    /**
     * 设置Item 长按、点击事件
     */
    public void setOnItemListener(OnItemClickListener<Data> listener) {
        this.listener = listener;
    }
}
